#pragma once
#include <iostream>
#include <string>

namespace NSTask
{
    class task
    {
    private:
        int _sock;

    public:
        task() = default;
        task(int sock)
            : _sock(sock)
        {
        }

        void Run()
        {
#define NUM 1024
            char buf[NUM] = {0};

            // while (true)
            // {
            bzero(buf, NUM);
            ssize_t sz = read(_sock, buf, NUM - 1);

            if (sz > 0)
            {
                buf[sz] = 0;

                std::cout << "client# " << buf << std::endl;

                std::string s;

                s += ">>server<< ";
                s += buf;

                write(_sock, s.c_str(), s.size());
            }
            else if (sz == 0)
            {
                std::cout << "client quit" << std::endl;
                // break;
            }
            else
            {
                std::cerr << "read error" << std::endl;
                // break;
            }

            close(_sock);
        }

        void operator()()
        {
            Run();
        }
    };
}